<template>
	<view class="mainbgc">
		<header-top :messageInfo="{typeFlag:true,message:'打印机绑定',liner:false}"></header-top>
		<view class="mainpadding" style="padding-bottom: 150rpx;">
			<u-radio-group v-model="dy_type" placement="row" circle>
				<u-radio activeColor="#0BBDCB" :customStyle="{marginRight: '30rpx'}" label="针孔打印机"
					name="1"></u-radio>
				<u-radio activeColor="#0BBDCB" label="热敏打印机" name="2"></u-radio>
			</u-radio-group>
			<view class="mainpadding_top ffffff radius" :class="checkid==item.id?'border':'fffborder'" v-for="item in list" :key="item.id" @click="checkid=item.id">
				<view class="flexbetween">
					<view class="">
						<view class="titletext">{{item.sn}}</view>
						<view class="nostrongtext margin_top2">{{item.name}}</view>
					</view>
					<view class="xiaolan" v-show="item.id==bind_id">已绑定</view>
					<view class="xiaohong" v-show="item.id!=bind_id">未绑定</view>
				</view>
			</view>
		</view>
		<view class="yesbtn" @click="bindclick">绑定</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				dy_type:"1",
				code:"",
				printer:"",
				list:[],
				checkid:"",
				bind_id:"",
			}
		},
		onLoad() {
			if(uni.getStorageSync("dy_type")){
				this.dy_type = uni.getStorageSync("dy_type").toString()
			}
			this.getInfo()
		},
		watch:{
			dy_type:{
				deep:true,
				handler(newval){
					this.init()
				}
			}
		},
		methods: {
			getInfo() {
				httpRequest.request('/dhtapi/user/index', 'POST', {}).then(res => {
					this.bind_id =  res.data.zs_printer_id
					this.init()
				}).catch(err => {
					httpRequest.toast(err.msg)
				})
			},
			init(){
				httpRequest.request('/dhtapi/user/printerList', 'GET', {
					type:this.dy_type
				}, false, false, true).then(res => {
					this.list = res.data
				})
			},
			bindclick(){
				httpRequest.request('/dhtapi/user/bindZsPrinter', 'GET', {
					id:this.checkid,
					dy_type:this.dy_type
				}, false, false, true).then(res => {
					httpRequest.toast(res.msg)
					if(res.code==1){
						uni.setStorageSync("dy_type",this.dy_type)
						setTimeout(()=>{
							uni.navigateBack(1)
						},500)
					}
				})
			},
		}
	}
</script>

<style>
	.border{
		border: 1rpx solid #00b855;
	}
	.fffborder{
		border: 1rpx solid #fff;
	}
</style>
